package com.example.sfm.controller.admin;

import com.example.sfm.constant.MessageConstants;
import com.example.sfm.pojo.dto.AnnouncementDto;
import com.example.sfm.pojo.dto.AnnouncementQueryDto;
import com.example.sfm.pojo.vo.AnnouncementVo;
import com.example.sfm.result.PageResult;
import com.example.sfm.result.Result;
import com.example.sfm.service.IAnnouncementsService;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sfm
 * @since 2025-03-07
 */
@Tag(name = "管理员公告管理")
@RestController("adminAnnouncementsController")
@RequestMapping("/admin/announcements")
@RequiredArgsConstructor
@Slf4j
public class AnnouncementsController {

    private final IAnnouncementsService announcementService;

    /**
     * 创建公告
     *
     * @param announcementDto 公告信息
     * @return 操作结果
     */
    @Operation(summary = "创建公告")
    @PostMapping("/create")
    public Result<?> createAnnouncement(@RequestBody @Validated @Parameter(description = "公告信息") AnnouncementDto announcementDto) {
        log.info("管理员创建公告: {}", announcementDto);
        announcementService.createAnnouncement(announcementDto);
        return Result.success(MessageConstants.ADD_OK);
    }

    /**
     * 获取所有公告
     *
     * @return 公告列表
     */
    @Operation(summary = "获取所有公告")
    @GetMapping("/list")
    public Result<PageResult<?>> listAnnouncements(@ParameterObject AnnouncementQueryDto queryDto) {
        log.info("管理员获取公告列表");
        return Result.success(announcementService.listAnnouncements(queryDto), MessageConstants.SELECT_OK);
    }

    /**
     * 更新公告
     *
     * @param id              公告ID
     * @param announcementDto 更新内容
     * @return 操作结果
     */
    @Operation(summary = "更新公告")
    @PutMapping("/update/{id}")
    public Result<?> updateAnnouncement(@PathVariable @Parameter(description = "公告ID") Long id, @RequestBody @Validated @Parameter(description = "更新内容") AnnouncementDto announcementDto) {
        log.info("管理员更新公告: ID={}, 内容={}", id, announcementDto);
        announcementService.updateAnnouncement(id, announcementDto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 批量删除公告
     *
     * @param ids 公告ID列表
     * @return 操作结果
     */
    @Operation(summary = "批量删除公告")
    @DeleteMapping("/delete")
    public Result<?> deleteAnnouncements(@RequestParam @Parameter(description = "公告ID列表",in = ParameterIn.QUERY) List<Long> ids) {
        log.info("管理员批量删除公告: IDs={}", ids);
        announcementService.deleteAnnouncements(ids);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }
}